package javbot.mail;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;

public class MailSender {
    
    public static String subject = "";
    public static String msgText = ""; 

    public static void sendEmail(String to, String from, String host,
            ArrayList<File> files, boolean debug) {

    	String errorMessage = null;
    	
        // create some properties and get the default Session
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);

        Session session = Session.getInstance(props, null);
        session.setDebug(debug);

        try {
            // create a message 
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = { new InternetAddress(to) };
            msg.setRecipients( Message.RecipientType.TO, address);
            msg.setSubject(subject);

            // create the Multipart
            Multipart mp = new MimeMultipart();
            MimeBodyPart mbp;

            // create and fill the first message part 
            mbp = new MimeBodyPart();
            mbp.setText(msgText);
            mp.addBodyPart(mbp);

            // create the second message part
            Iterator<File> fileIt = files.iterator(); 
            
            while(fileIt.hasNext()){
                File file = (File) fileIt.next();
                mbp = new MimeBodyPart();
                FileDataSource fds = new FileDataSource(file.getName ());
                mbp.setDataHandler(new DataHandler(fds));
                mbp.setFileName(fds.getName());
                mp.addBodyPart(mbp);
            }

            // add the Multipart to the message 
            msg.setContent(mp);

            // set the Date: header
            msg.setSentDate(new Date());

            // send the message
            Transport.send(msg);

        } catch (MessagingException mex) {
        	errorMessage = "Error sending email\n" + mex.getMessage() + "\n";
            mex.printStackTrace();
            Exception ex = null;
            if ((ex = mex.getNextException()) != null) {
                ex.printStackTrace();
                errorMessage += ex.getMessage();
            }
            

            
        } finally {
        	if (errorMessage != null)
	    		JOptionPane.showMessageDialog(null,
	    			    errorMessage,
	    			    "Error",
	    			    JOptionPane.WARNING_MESSAGE);        	
        }
    }
}


